﻿using System;
using System.Data;
using System.ServiceModel;
using System.Threading;
using SLS.ExClassLib.RTIService;
using SLS.ExClassLib.Data;
using SLS.ExClassLib.RTIService;
using SLS.ExClassLib.Event;
using SLS.ExClassLib.Events;
using SLS.ExClassLib.Helpers;
using SLS.ExClassLib.Message;
using SLS.ExClassLib.WCFClient;
namespace SLS.Base
{
    [CallbackBehavior(
    ConcurrencyMode = ConcurrencyMode.Multiple,
    UseSynchronizationContext = false)]
    public partial class SLBC : RTIServiceCallback, WorkFlowCenterServiceCallBack
    {
        private RTIServiceClient m_RTIClient;
        public RTIServiceClient RTIClient
        {
            get
            {
                if (m_RTIClient == null)
                {
                    m_RTIClient = new RTIServiceClient(new InstanceContext(this), "TcpBinding");
                    m_RTIClient.Open();
                    _uiSyncContext = SynchronizationContext.Current;
                    if (ContactRTIService.Instance.MasterRTIServiceClient == null)
                    {
                        ContactRTIService.Instance.MasterRTIServiceClient = m_RTIClient;
                    }
                }
                return m_RTIClient;
            }
            set
            {
                m_RTIClient = value;
            }
        }
         
        public delegate void NewJobDelegate(MessageEx msg);
        public virtual void StartFedrate()
        {
            MessageEx msg = null;
            msg = new MessageEx(Address, "HLAFederation", "OnJoinFederation", "NO");
            msg.TimeStamp = ContactRTIService.Instance.GetRTITimeStamp(msg);
            object data = RTIClient;
            EventDispatcher.Instance.Publish(msg, data);
        } 
        [CLSCompliantAttribute(false)]
        public virtual void HandleMAMessage(MessageEx msg)
        {
            string AgentID = msg.AgentID;
            _uiSyncContext = SynchronizationContext.Current;
            SendOrPostCallback callback =
                delegate(object state)
                {
                    EventDispatcher.Instance.Publish(msg, null,EventType.RTI2FED);
                }; 
            _uiSyncContext.Post(callback, AgentID);
        }
        [CLSCompliantAttribute(false)]
        public virtual void NotifyWFClient(MessageEx msg)
        {
            string AgentID = msg.AgentID;
            SendOrPostCallback callback =
                delegate(object state)
                {
                    //EventDispatcher.Instance.RECVWF2UIMessage(msg); 
                    msg.JobType = "WF2UI";
                    object data = null;
                    EventDispatcher.Instance.Publish(msg, data);
                };
            _uiSyncContext.Post(callback, AgentID);
        } 
        public void ProcessRTIMessage(MessageEx msg)
        {
            try
            {
                string JobID = msg.JobID;
                DataSet setEvent = msg.setEvent;
                DataRow dwEvent;
                Tracer.Verbose(Name + Address + ": ", JobID);
                switch (JobID)
                {
                    case "OnJoinedFederation":
                        //LoadCreatedRecord(setEvent);
                        Tracer.Verbose(this.Address,"JoinedFederation"+msg.TimeStamp);
                        break; 
                    default:
                        break;
                }
            }
            catch (Exception ex)
            {
                throw;
            }
        }
        public virtual void ProcessUIMessage(MessageEx msg)
        {
            try
            {
                using (new LongOperation(this))
                {
                    string JobID = msg.JobID;
                    DataSet setEvent = msg.setEvent;
                    DataRow dwEvent;
                    ExDataRow row = null;
                    Tracer.Verbose(Name + Address + ": ", JobID);
                    switch (JobID)
                    {
                        case "OnLoadCreatedRecord":
                            LoadCreatedRecord(setEvent, msg.CallerUID, msg);
                            break;
                        case "OnSaveActiveRecord":
                            //SaveActiveRecord(setEvent, msg);
                            break;
                        case "OnSetUpSet5Property":
                            SetUpSet5Property(setEvent, msg.CallerUID);
                            break;
                        case "OnLoadForeignKeyTable":                        
                            row = (ExDataRow)msg.Tag;
                            row.LoadLifeData(1);
                            break;
                        case "OnLoadSet2Create":
                            row = (ExDataRow)msg.Tag;
                            row.BuildSet2Create(true);
                            break;
                        case "OnLoadSet4Reference":
                            row = (ExDataRow)msg.Tag;
                            row.BuildSet4Reference();
                            break;
                        case "OnLoadSet5KeyDef":
                            row = (ExDataRow)msg.Tag;
                            row.BuildSet5KeyDef();
                            break;
                        case "OnSetDefaultActiveRecord":
                            SetDefaultActiveRecord();
                            break;
                        case "OnKeyRelaitonComplete":
                            ViewDataSet.ExecuteKeyRelaitonComplete(setEvent);
                            SetDefaultActiveRecord(setEvent);
                            break;
                        default:
                            break;
                    }
                    /*MessageEx msgN = new MessageEx(Address, "UIFederation", "OnGetNextMessage", "NO");
                    msgN.TimeStamp = DateTime.Now;
                    EventDispatcher.Instance.POSTUI2UIMessage(msgN, null);
                    */
                }
            }
            catch (Exception ex)
            {
                throw;
            }
        }  
    }
}